package com.bob.shirodemo.base.web;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限拦截器
 */
@Slf4j
public class SecurityInterceptor implements HandlerInterceptor {

//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String requestURI = request.getRequestURI();
//        log.info("==========>>>request uri:{}", requestURI);
//        Subject subject = SecurityUtils.getSubject();
//        if(subject.isAuthenticated()) {
//            return true;
//        }else {
//            UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
//            token.setRememberMe(true);
//            try {
//                subject.login(token);
//            }catch ( UnknownAccountException uae ) {
//                log.error("==========>>>logging occurred a error：", uae);
//                //username wasn't in the system, show them an error message?
//            } catch ( IncorrectCredentialsException ice ) {
//                //password didn't match, try again?
//                log.error("==========>>>logging occurred a error：", ice);
//            } catch ( LockedAccountException lae ) {
//                //account for that username is locked - can't login.  Show them a message?
//                log.error("==========>>>logging occurred a error：", lae);
//            }
//            if(subject.isAuthenticated()) {
//                return true;
//            }
//        }
//        return false;
//    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        log.info("==========>>>request uri:{}", requestURI);
        Subject subject = SecurityUtils.getSubject();
        if(requestURI.equals("/login")) {
            return true;
        }

        Session session = SecurityUtils.getSubject().getSession(false);
        if(session != null) {
            String token1 = (String) SecurityUtils.getSubject().getSession(false).getAttribute("token");
            String name = (String) SecurityUtils.getSubject().getSession(false).getAttribute("name");
        }
        if(subject.isAuthenticated()) {
            return true;
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
